// pages/card/card.js
const app = getApp();
let memberId = '', poodId='',codeImg='',ctx;
function getImageInfo(url) {
  return new Promise((resolve, reject) => {
    wx.getImageInfo({
      src: url,
      success: resolve,
      fail: reject,
    })
  })
}
const indexList = (that) => {
  wx.request({
    header: { 'content-type': 'application/x-www-form-urlencoded' },
    method: 'POST',
    url: 'https://interface.globalhexi.cn/gift/messagePush/rwafermtr',
    data: { tuanGoodsId: poodId, memberId: memberId },
    success: function (res) {
      console.log(res.data)
      let goods = res.data;
      let oP = '￥' + (goods.intPrice / 100).toFixed(2);
      let nP = '￥' + (goods.intPinPrice / 100).toFixed(2);
      let npL = (nP.length - 1) * 20;
      let Url = `https://www.globalhexi.com/${goods.goodsImg}`;
      let good1 = goods.pingName.substring(0, 12)
      let good2 = goods.pingName.substring(12, 24)
      let good3 = goods.pingName.substring(24, 36)
      codeImg = goods.erWeiMaUrl;
      const urlPromise = getImageInfo(Url)
      const codePromise = getImageInfo(codeImg)
      Promise.all([urlPromise, codePromise]).then(([url, code]) => {
        ctx.rect(0, 0, 280, 400)
        ctx.setFillStyle('#fff')
        ctx.fill()
        
        ctx.beginPath()
        ctx.setFillStyle('#ff383d')
        ctx.setFontSize(20)
        ctx.fillText(nP, 15, 370)
        ctx.closePath()

        ctx.beginPath()
        ctx.drawImage(code.path, 40, 30, 200, 200)
        ctx.drawImage('../../image/k_06.png', 192, 250, 90, 30)
        ctx.setFillStyle('#333')
        ctx.setFontSize(14)
        ctx.fillText(good1, 15, 280)
        ctx.fillText(good2, 15, 300)
        ctx.fillText(good3, 15, 320)
        ctx.fillText('扫描/长按识别', 180, 385)
        ctx.closePath()

        ctx.beginPath()
        ctx.setFillStyle('#666')
        ctx.setFontSize(13)
        ctx.fillText(oP, npL, 385)
        ctx.closePath()

        ctx.beginPath()
        ctx.drawImage(url.path, 200, 310, 60, 60)
        ctx.drawImage('../../image/k_04.png', npL, 350, 50, 18)
        ctx.setStrokeStyle('#666')
        ctx.setLineWidth(1)
        ctx.moveTo(npL - 5, 380)
        ctx.lineTo(npL + 60, 380)
        ctx.stroke()

        ctx.draw(false, function () {
          wx.canvasToTempFilePath({
            x: 0,
            y: 0,
            width: 280,
            height: 400,
            destWidth: 280,
            destHeight: 400,
            canvasId: 'firstCanvas',
            success(res) {
              wx.hideLoading()
              console.log(res.tempFilePath)
              that.data.targetSharePath = res.tempFilePath;
            }
          })
        })
      })
    }
  })
};
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowHeight: app.globalData.windowHeight,
    wHeight:'',
    boolT:true,
    targetSharePath:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    poodId = options.id;
    ctx = wx.createCanvasContext('firstCanvas')
    memberId = wx.getStorageSync('memberId') || '';
    wx.showLoading({title: '加载中',})
    indexList(that);
    wx.getSystemInfo({
      success(res) {
        that.data.wHeight = (res.windowWidth - 280) / 2;
        that.setData({
          wHeight: that.data.wHeight
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
  },
  getImage:function(e){
    let that = this;
    if (that.data.boolT) {
      that.data.boolT=false;
      wx.getSetting({
        success: function (res) {
          if (res.authSetting['scope.writePhotosAlbum']) {//已授权
            that.saveImageToPhotosAlbum();
          } else {
            wx.authorize({
              scope: 'scope.writePhotosAlbum',
              success(res) {//用户确认授权
                that.saveImageToPhotosAlbum();
              },
              fail() {//用户取消授权
                wx.showModal({
                  title: '提示',
                  content: '你需要授权才能保存图片到相册',
                  success: function (res) {
                    if (res.confirm) {
                      wx.openSetting({//手动设置系统权限
                        success: function (res) {
                          if (res.authSetting['scope.writePhotosAlbum']) {
                            that.saveImageToPhotosAlbum();
                          } else {
                            //用户未同意保存图片权限
                            setTimeout(function () { that.data.boolT = true; }, 500)
                          }
                        },
                        fail: function () {
                          //用户未同意保存图片权限
                          setTimeout(function () { that.data.boolT = true; }, 500)
                        }
                      })
                    }
                  }
                })
              }
            })
          }
        }
      })
    }
    
  },
  saveImageToPhotosAlbum: function () {
    let that = this;
    wx.saveImageToPhotosAlbum({
      filePath: that.data.targetSharePath,
      success: function () {
        wx.showToast({
          title: '已保存到系统相册',
          icon: 'success',
          duration: 2000
        })
        setTimeout(function () { that.data.boolT = true; }, 500)
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})